package com.wechat.back.controller;

import com.wechat.back.anno.OptLog;
import com.wechat.back.pojo.Admin;
import com.wechat.back.pojo.tools.Result;
import com.wechat.back.service.AdminService;
import com.wechat.back.utils.JwtUtil;
import com.wechat.back.utils.Md5Util;
import com.wechat.back.utils.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/admin")
@Validated
public class AdminController {
    @Autowired
    private AdminService adminService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @OptLog(optModule = "管理员模块",optType = "登录", optDesc = "管理员登录")
    @PostMapping("/login")
    public Result<String> login(@Pattern(regexp = "^\\S{5,16}$") String adminName,@Pattern(regexp = "^\\S{5,16}$") String password){
        //根据用户名查询用户
        Admin login_user = adminService.findByAdminName(adminName);
        if(login_user==null){
            return Result.error("用户名不存在");
        }
        //加密密码与数据库进行比较
        if(Md5Util.getMD5String(password).equals(login_user.getPassword())){
            //token失效时间12小时
            Map<String, Object> claims = new HashMap<>();
            claims.put("adminId",login_user.getAdminId());
            claims.put("adminName", login_user.getAdminName());
            String token = JwtUtil.genToken(claims);
            //redis
            ValueOperations<String,String> operations = stringRedisTemplate.opsForValue();
            operations.set(token,token,12, TimeUnit.HOURS);//失效12小时
            return Result.success(token);
        }
        return Result.error("密码错误");
    }

    @OptLog(optModule = "管理员模块",optType = "注册", optDesc = "管理员注册")
    @RequestMapping("/register")
    public Result register(@Pattern(regexp = "^\\S{5,16}$") String adminName,@Pattern(regexp = "^\\S{5,16}$") String password){
        //查询用户
        Admin u = adminService.findByAdminName(adminName);
        System.out.println(u);
        if(u==null){
            //没有占用，注册
            adminService.register(adminName, password);
            return Result.success();
        }else {
            //占用
            return Result.error("用户名已占用");
        }
    }

    @GetMapping("/adminInfo")
    public Result<Admin> adminInfo(){
        Map<String, Object> map = ThreadLocalUtil.get();
        String adminName = (String) map.get("adminName");
        Admin admin = adminService.findByAdminName(adminName);
        return Result.success(admin);
    }
}
